package com.qf.controller;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet("/upload")
@MultipartConfig//获取前端的文件上传组件的中内容(将文件"二进制数据"进行解析)
public class UpdateLoadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //获取商品的名称--普通表单数据
        String pname = request.getParameter("pname");
        System.out.println(pname) ;


        //1)通过前端的文件上传组件的name="xx" 获取上次解析器Part
        Part part = request.getPart("file");//参数必须为name属性值
        //2)获取上传的文件名称
        String fileName = part.getSubmittedFileName();
        System.out.println(fileName) ;

        //3)通过请求对象获取全局对象ServletContext--->获取products所在的文件夹路径 (绝对路径)
        String realPath = request.getServletContext().getRealPath("/products");
        System.out.println(realPath) ;

        //4)使用上传解析器Part 写入到realPath中
        part.write(realPath+"/"+fileName);

        response.getWriter().write("上传成功!") ;


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
